const router = require('express').Router()
const { ScreeningTimes, Order, User, Movie } = require('@/models')
const { data } = require('@/utils/send')

router.get('/', async (req, res) => {
    try {
        const order = await Order.findAll()
        const TimesId = order.map(item => item.event_id)
        const UsersId = order.map(item => item.user_id)
        const users = await Promise.all(UsersId.map(item => getUser(item)))
        const events = await Promise.all(TimesId.map(item => getTime(item)))
        console.log(events, users)
        const MovieId = events.map(item => item.movie_id)
        const movies = await Promise.all(MovieId.map(async (item) => await getMovie(item)))
        order.forEach((e, index) => {
            e.dataValues.user = users[index]
            e.dataValues.event = events[index]
            e.dataValues.movie = movies[index]
        });;
        if (!order) return res.cc('订单获取失败')
        const datas = data({ message: '订单获取成功', data: order })
        res.send(datas)
        res.locals.data = datas
    } catch (error) {
        res.clearCookie(error)
    }
})
async function getTime(id) {
    return await ScreeningTimes.findOne({ where: { id } })
}
async function getMovie(id) {
    return await Movie.findOne({ where: { id } })
}
async function getUser(id) {
    return await User.findOne({ where: { id } })
}
module.exports = router
